//
//  HWStatus.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/11.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWStatus.h"
#import "MJExtension.h"
#import "HWPhoto.h"
#import "NSDate+Tool.h"
@implementation HWStatus
- (NSDictionary *)objectClassInArray{
    return @{@"pic_urls": [HWPhoto class]};
}

- (NSString *)created_at{
    /**
     *  EEE MMM dd HH:mm:ss Z YYYY
     */
    // E:星期几
    // M:月份
    // d:几号(这个月的第几天)
    // H:24小时制的小时
    // m:分钟
    // s:秒
    // y:年
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    //微博创建时间
    NSDate *createDate = [fmt dateFromString:_created_at];
    //当前时间
    NSDate *now = [NSDate date];
    //日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    //计算两个日期的差值
    NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];
//    NSDateComponents *createDateCmps = [calendar components:unit fromDate:createDate];
    
    if ([createDate isThisYear]) {//今年
        if ([createDate isYesterday]) {//
            fmt.dateFormat = @"昨天HH:mm";
            return [fmt stringFromDate:createDate];
        }else if ([createDate isToday]){
            if (cmps.hour >= 1) {
            return [NSString stringWithFormat:@"%ld小时前",cmps.hour];
            }else if (cmps.minute >= 1){
            return [NSString stringWithFormat:@"%ld分钟前",cmps.minute];
            }else {
                return @"刚刚";
}
        }else{//今年其他日子
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }
    }else{//非今年
    fmt.dateFormat = @"yyyy-MM-dd HH:mm";
   return [fmt stringFromDate:createDate];
    }
}
/**
 *  <a href="http://app.weibo.com/t/feed/3o33sO" rel="nofollow">iPhone 6</a>
 */
- (void)setSource:(NSString *)source{
    if (source.length) {
        NSRange range;
        range.location = [source rangeOfString:@">"].location + 1;
        range.length = [source rangeOfString:@"</"].location - range.location;
        _source = [NSString stringWithFormat:@"来自%@",[source substringWithRange:range]];
    }else{
    _source = @"来自未知";
    }
}
///**是不是今年*/
//- (BOOL)isThisYear:(NSDate *)date{
//    //日历对象
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSCalendarUnit unit = NSCalendarUnitYear;
//    //获得某个时间的年月日时分秒
//    NSDateComponents *dateCmps = [calendar components:unit fromDate:date];
//    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
//    return dateCmps.year == nowCmps.year;
//}
///**是不是昨天*/
//- (BOOL)isYesterday:(NSDate *)date{
//    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//    fmt.dateFormat = @"yyyy-MM-dd";
//    NSDate *now = [NSDate date];
//    NSString *createStr = [fmt stringFromDate:date];
//    NSString *nowStr = [fmt stringFromDate:now];
//    date = [fmt dateFromString:createStr];
//    now = [fmt dateFromString:nowStr];
//    //日历对象
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//    //计算两个日期的差值
//    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:now options:0];
//    return cmps.year == 0 && cmps.month == 0 && cmps.day == 1;
//}
///**是不是今天*/
//- (BOOL)isToday:(NSDate *)date{
//    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//    fmt.dateFormat = @"yyyy-MM-dd";
//    NSDate *now = [NSDate date];
//    NSString *createStr = [fmt stringFromDate:date];
//    NSString *nowStr = [fmt stringFromDate:now];
//    date = [fmt dateFromString:createStr];
//    now = [fmt dateFromString:nowStr];
//    //日历对象
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//    //计算两个日期的差值
//    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:now options:0];
//  
//    
//    return cmps.year == 0 && cmps.month == 0 && cmps.day == 0;
//  
//}
@end
